// src/stores/userStore.ts
import { defineStore } from "pinia";

import type { Status } from "@/types/Status";
import { HomeAPI } from "@/api/HomeAPI";
import ajaxsuccess from "@/utils/ajaxsuccess";
import type { navList, navtag } from "@/types/navigation";
import { ElMessage, ElMessageBox } from "element-plus";


type State = {
  navtaglist:{
    result: navtag[];
    status: Status;
  },
  navList:{
    result: navList[];
    collent_arr:number[]
    status: Status;

  }
};

type Getters = {};

type Actions = {
   //添加导航标签
   Navaigation_add(navtag:navtag):Promise<boolean>
   //获取导航标签
   get_Navigation():Promise<void>
   //删除标签
   delete_Navaigation(nid:number):Promise<void>
    //获取导航信息
   get_Navlist(navtag:navtag):Promise<void>
   //添加导航信息
   add_Navsmg(navList:navList):Promise<boolean>
   //删除导航信息
   delete_Navsmg(nid:number):Promise<void>

};

export const navigationStore = defineStore<"navigation", State, Getters, Actions>(
  "navigation",
  {
    state: () => ({
        navtaglist:{
        result: [],
        status:'idle'
      },
      navList:{
        result: [],
        collent_arr:[],
        status: "idle",
      }

    }
     
     ),
     actions:{
      //添加导航标签
      async  Navaigation_add(navtag:navtag){
         //发送请求
         const response = await HomeAPI.Navigation_add(navtag)
         //调用弹出函数
         const relboe =  ajaxsuccess(response)
         if(!relboe){
         this.get_Navigation()
          return relboe
         }
         return relboe
      },
      //获取导航标签
      async get_Navigation(){
              //更新请求状态
        this.navtaglist.status = "loading"
        try{
         //发送请求获取数据
        const response =  await HomeAPI.get_Navigation()
      
        //保存数据
        this.navtaglist.result = response.data
        //更改状态为成功态
        this.navtaglist.status = "success"
        }catch(error){
        //更改状态为失败态
        this.navtaglist.status = 'error'
        }
      },
      //删除导航标签
      async delete_Navaigation(nid){
             
      ElMessageBox.confirm("确认删除此导航信息吗?", "导航信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        const response= await HomeAPI.delete_Navigation(nid)
        //调用弹出函数
        const relboe =  ajaxsuccess(response)
        if(!relboe){
          this.get_Navigation()
          return relboe
        }
       
      })
      .catch(() => {
        ElMessage({
          type: "info",
          message: "取消删除",
        });
      });
      },
      
      //获取导航信息
      async get_Navlist(navtag:navtag){
       
        
        //更新请求状态
        this.navList.status = "loading"
        try{
         //发送请求获取数据
        const response =  await HomeAPI.get_NavLists(navtag)
        console.log(response);
      
        
        //保存数据
        this.navList.result = response.data
        this.navList.collent_arr = response.collent_arr
        //更改状态为成功态
        this.navList.status = "success"
        }catch(error){
        //更改状态为失败态
        this.navList.status = 'error'
        }
      },
      //添加导航信息
     
      async add_Navsmg(navList:navList){
        //发送请求
        const response = await HomeAPI.add_Navsmg(navList)
        //调用弹出函数
        const relboe =  ajaxsuccess(response)
        return relboe
     },
    async delete_Navsmg(nid:number){
      ElMessageBox.confirm("确认删除此导航信息吗?", "导航信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        const response= await HomeAPI.delete_Navsmg(nid)
        //调用弹出函数
        const relboe =  ajaxsuccess(response)
        
       this.get_Navlist({title:'推荐',nid:2, order_by: 'create_date',})
        
       
      })
      .catch(() => {
        ElMessage({
          type: "info",
          message: "取消删除",
        });
      });
     }

     },
 
  }
);